//
//  UIView+Tool.swift
//  YuanJuBian
//
//  Created by PerhapYs on 2021/12/22.
//

import Foundation
import UIKit

extension UIView{
    
    /// 获取安全区域
    var safeAreaInsets_PY : UIEdgeInsets{
        get{
            if #available(iOS 11.0, *) {
                return self.safeAreaInsets
            } else {
                return .zero
            }
        }
    }
    /// 获取安全区域布局
    var safeAreaLayoutGuide_PY : UILayoutGuide{
        
        if #available(iOS 11.0, *) {
            return self.safeAreaLayoutGuide
        } else {
            return UILayoutGuide()
        }
    }

    func addBorderLine(lineFrame:CGRect,lineColor:UIColor){
        
        let lineLayer = CALayer()
        lineLayer.frame = lineFrame
        lineLayer.backgroundColor = lineColor.cgColor
        self.layer.addSublayer(lineLayer)
    }
    /// UIView添加渐变色
    /// - Parameters:
    ///   - frame: 渐变色大小
    ///   - startPoint: 起点
    ///   - endPoint: 终点
    ///   - colors: 渐变颜色集合
    ///   - locations: 渐变位置集合
    func gradientColor(frame:CGRect? = nil , startPoint:CGPoint , endPoint:CGPoint , colors:[String] , locations:[NSNumber]?) {
        
        let colorLayer = CAGradientLayer.init()
        colorLayer.frame = frame ?? self.bounds
        colorLayer.startPoint = startPoint
        colorLayer.endPoint = endPoint
        
        var layerColors : [Any] = []
        for colorString in colors {
            
            let color = colorString.HexColor()
            layerColors.append(color.cgColor)
        }
        colorLayer.colors = layerColors
        
        colorLayer.locations = locations
        self.layer.addSublayer(colorLayer)
    }
    
    /// 添加阴影
    /// - Parameters:
    ///   - offset: 阴影偏移
    ///   - color: 阴影颜色
    ///   - viewColor: 视图颜色，必须设置不然不出现阴影
    ///   - alpha: 阴影透明度
    ///   - width: 阴影宽度
    ///   - cornerRedius: 切角大小，不可使用clipsToBounds,masksToBounds,阴影不出现。
    func addShadow(with offset:CGSize = CGSize.init(width: 0, height: 1),color:UIColor,viewColor:UIColor? = UIColor.white,alpha:Float,width:CGFloat,cornerRedius:CGFloat = 0){
        
        self.layer.shadowOffset = offset
        self.layer.shadowColor = color.cgColor
        self.layer.shadowRadius = width
        self.layer.shadowOpacity = alpha
        self.backgroundColor = viewColor
        
        self.layer.cornerRadius = cornerRedius
    }
    /// 切指定角
    /// - Parameters:
    ///   - conrners: 切角位置
    ///   - radius: 切角大小
    func addCorner(conrners: UIRectCorner? = nil , radius: CGFloat) {
        if let conrners = conrners {
            let maskPath = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: conrners, cornerRadii: CGSize(width: radius, height: radius))
            let maskLayer = CAShapeLayer()
            maskLayer.frame = self.bounds
            maskLayer.path = maskPath.cgPath
            self.layer.mask = maskLayer
        }
        else{
            self.layer.cornerRadius = radius
            self.layer.masksToBounds = true
        }
    }
    enum PYTransformKeyPathType : String {
        case x = "transform.rotation.x"
        case y = "transform.rotation.y"
        case z = "transform.rotation.z"
    }
    func addTransform(keyPathType:PYTransformKeyPathType){
        
        let animation = CABasicAnimation.init(keyPath: keyPathType.rawValue)
        animation.toValue = Double.pi * 2
        animation.duration = 2
        animation.isRemovedOnCompletion = false
        animation.repeatCount = MAXFLOAT
        animation.fillMode = .forwards
        self.layer.add(animation, forKey: "transform")
    }
    func removeTransform(){
        
        self.layer.removeAnimation(forKey: "transform")
    }
}

extension UIStackView{
    
    func clearArrangedSubviews(){
        
        while let first = self.arrangedSubviews.first {
            first.removeFromSuperview()
        }
    }
}
